package com.gameley.dmgtpf.kernel.controller;

import com.gameley.dmgtpf.common.core.aspect.MyDoneTime;
import com.gameley.dmgtpf.common.utils.PubEnvUtil;
import com.gameley.dmgtpf.kernel.entity.GamelabelEntity;
import com.gameley.dmgtpf.kernel.service.GamelabelService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @author libencheng
 * @email libencheng@gmail.com
 * @date 2021-07-13 17:13:45
 */
@Controller
@RequestMapping("kernel/gamelabel")
public class GamelabelController
{
    @Autowired
    private GamelabelService gamelabelService;
    
    @MyDoneTime(param = "GamelabelController.tabletodb")
    @ResponseBody
    @RequestMapping("/tabletodb")
    public String file2tableSava(@RequestParam(value = "file", required = false) MultipartFile file)
    {
        String msg = gamelabelService.file2tableSavaDB(file);
        return msg;
    }
    
    /**
     * 列表
     */
    @MyDoneTime(param = "GamelabelController.list")
    @RequestMapping("/list")
    @RequiresPermissions("platform:gamelabel:list")
    public String list(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session)
    {
        
        if (PubEnvUtil.isEmptyObj(session.getAttribute("loginUser")))
        {
            return "index.html";
        }
        
        List<GamelabelEntity> list = gamelabelService.list();
        
        PubEnvUtil.setModel(model, list);
        
        return "platform/gamelabelist";
    }
    
    @MyDoneTime(param = "GamelabelController.addPage")
    @RequestMapping("/add")// 去增加员工界面
    public String addPage(Model model)
    {
        return "platform/gamelabeadd";
    }
    
    /**
     * 修改
     */
    @MyDoneTime(param = "GamelabelController.update")
    @RequestMapping("/update")
    @RequiresPermissions("root:gamelabel:update")
    // @RequestBody
    public String update(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session, GamelabelEntity entity)
    {
        PubEnvUtil.setUpdateAttrValue(entity);
        gamelabelService.updateById(entity);
        return list(request, response, model, session);
    }
    
    // 去修该员工界面
    @MyDoneTime(param = "GamelabelController.updatePage")
    @GetMapping("/update/{id}")
    public String updatePage(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id, Model model)
    {
        GamelabelEntity entity = gamelabelService.getById(id);
        model.addAttribute("temp", entity);
        return "platform/gamelabeupdate";
    }
    
    /**
     * 保存
     */
    @MyDoneTime(param = "GamelabelController.save")
    @RequestMapping(value = "/save")
    @RequiresPermissions("root:gamelabel:save")
    public String save(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session, GamelabelEntity entity)
            throws Exception
    {
        gamelabelService.saveLabel(entity);
        return list(request, response, model, session);
    }
    
    // 删除员工
    @MyDoneTime(param = "GamelabelController.deleteMethod")
    @GetMapping("/delete/{id}")
    public String deleteMethod(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable("id") Integer id)
    {
        gamelabelService.removeByIds(Arrays.asList(id));
        List<GamelabelEntity> admins = gamelabelService.list();
        model.addAttribute("temps", admins);
        
        return "platform/gamelabelist";
    }
    
}
